<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三方施工检测模型</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
            color: #333;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
        }
        .upload-area {
            border: 2px dashed #ccc;
            border-radius: 8px;
            padding: 30px;
            text-align: center;
            margin-bottom: 20px;
            cursor: pointer;
            transition: border-color 0.3s;
        }
        .upload-area:hover {
            border-color: #3498db;
        }
        .upload-area.dragover {
            border-color: #3498db;
            background-color: #f0f8ff;
        }
        #file-input {
            display: none;
        }
        .preview-container {
            margin-top: 20px;
            text-align: center;
            position: relative;
        }
        #image-preview {
            max-width: 100%;
            max-height: 400px;
            display: none;
            margin: 0 auto;
            border-radius: 5px;
            border: 1px solid #ddd;
        }
        .btn {
            background-color: #3498db;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-top: 15px;
            transition: background-color 0.3s;
        }
        .btn:hover:not(:disabled) {
            background-color: #2980b9;
        }
        .btn:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        .results-container {
            margin-top: 30px;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 5px;
            display: none;
        }
        .no-events {
            text-align: center;
            padding: 20px;
            color: #27ae60;
            font-weight: bold;
        }
        .event-card {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            border-left: 5px solid #e74c3c;
        }
        .event-card.medium-risk {
            border-left-color: #f39c12;
        }
        .event-card.low-risk {
            border-left-color: #27ae60;
        }
        .event-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        .event-type {
            font-size: 18px;
            font-weight: bold;
            color: #2c3e50;
        }
        .event-level {
            padding: 4px 12px;
            border-radius: 20px;
            font-weight: bold;
            font-size: 14px;
        }
        .high-risk {
            background-color: #ffebee;
            color: #e74c3c;
        }
        .medium-risk {
            background-color: #fff3e0;
            color: #f39c12;
        }
        .low-risk {
            background-color: #e8f5e9;
            color: #27ae60;
        }
        .event-details {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
            margin-bottom: 15px;
        }
        .detail-item {
            display: flex;
            flex-direction: column;
        }
        .detail-label {
            font-size: 12px;
            color: #7f8c8d;
            margin-bottom: 4px;
        }
        .detail-value {
            font-size: 14px;
            font-weight: 500;
        }
        .confidence-bar {
            height: 20px;
            background-color: #f0f0f0;
            border-radius: 10px;
            margin-top: 5px;
            overflow: hidden;
            position: relative;
        }
        .confidence-fill {
            height: 100%;
            background-color: #3498db;
            border-radius: 10px;
            transition: width 0.5s ease-in-out;
        }
        .confidence-text {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            text-align: center;
            color: white;
            font-weight: bold;
            font-size: 12px;
            line-height: 20px;
        }
        .event-description {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            margin-top: 15px;
            border-left: 3px solid #3498db;
        }
        .loading {
            text-align: center;
            margin: 20px 0;
            display: none;
        }
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #3498db;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .detection-overview {
            background-color: #e3f2fd;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            text-align: center;
            font-weight: bold;
        }
        .detected {
            color: #e74c3c;
        }
        .not-detected {
            color: #27ae60;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>三方施工检测模型</h1>
        
        <div class="upload-area" id="upload-area">
            <p>点击或拖拽图片到这里上传</p>
            <input type="file" id="file-input" accept="image/*">
        </div>
        
        <div class="preview-container">
            <img id="image-preview" alt="图片预览">
        </div>
        
        <div style="text-align: center;">
            <button class="btn" id="process-btn" disabled>开始事件检测</button>
        </div>
        
        <div class="loading" id="loading">
            <div class="spinner"></div>
            <p>模型分析中，请稍候...</p>
        </div>
        
        <div class="results-container" id="results-container">
            <h2>检测结果</h2>
            <div id="detection-overview" class="detection-overview"></div>
            <div id="events-list"></div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const uploadArea = document.getElementById('upload-area');
            const fileInput = document.getElementById('file-input');
            const imagePreview = document.getElementById('image-preview');
            const processBtn = document.getElementById('process-btn');
            const loading = document.getElementById('loading');
            const resultsContainer = document.getElementById('results-container');
            const eventsList = document.getElementById('events-list');
            const detectionOverview = document.getElementById('detection-overview');
            
            let uploadedFile = null;
            
            // 点击上传区域触发文件选择
            uploadArea.addEventListener('click', function() {
                fileInput.click();
            });
            
            // 拖拽功能
            uploadArea.addEventListener('dragover', function(e) {
                e.preventDefault();
                uploadArea.classList.add('dragover');
            });
            
            uploadArea.addEventListener('dragleave', function() {
                uploadArea.classList.remove('dragover');
            });
            
            uploadArea.addEventListener('drop', function(e) {
                e.preventDefault();
                uploadArea.classList.remove('dragover');
                
                if (e.dataTransfer.files.length) {
                    handleFile(e.dataTransfer.files[0]);
                }
            });
            
            // 文件选择变化
            fileInput.addEventListener('change', function() {
                if (fileInput.files.length) {
                    handleFile(fileInput.files[0]);
                }
            });
            
            // 处理文件上传
            function handleFile(file) {
                if (!file.type.match('image.*')) {
                    alert('请上传图片文件！');
                    return;
                }
                
                uploadedFile = file;
                
                // 显示预览
                const reader = new FileReader();
                reader.onload = function(e) {
                    imagePreview.src = e.target.result;
                    imagePreview.style.display = 'block';
                    processBtn.disabled = false;
                    
                    // 清空之前的结果
                    resultsContainer.style.display = 'none';
                    eventsList.innerHTML = '';
                    detectionOverview.innerHTML = '';
                };
                reader.readAsDataURL(file);
            }
            
            // 处理按钮点击
            processBtn.addEventListener('click', function() {
                if (!uploadedFile) return;
                
                const formData = new FormData();
                formData.append('file', uploadedFile);
                
                // 显示加载中
                loading.style.display = 'block';
                resultsContainer.style.display = 'none';
                processBtn.disabled = true;
                
                // 发送请求到后端
                fetch('/upload', {
                    method: 'POST',
                    body: formData
                })
                .then(response => response.json())
                .then(data => {
                    loading.style.display = 'none';
                    
                    if (data.success) {
                        displayResults(data.detection_result);
                    } else {
                        alert('处理失败: ' + data.error);
                        processBtn.disabled = false;
                    }
                })
                .catch(error => {
                    loading.style.display = 'none';
                    alert('请求出错: ' + error);
                    processBtn.disabled = false;
                });
            });
            
            // 显示检测结果
            function displayResults(result) {
                eventsList.innerHTML = '';
                
                // 显示检测概览
                if (result.is_happen && result.events.length > 0) {
                    detectionOverview.innerHTML = `检测到 <span class="detected">${result.events.length} 个事件</span>`;
                } else {
                    detectionOverview.innerHTML = `<span class="not-detected">未检测到任何事件</span>`;
                }
                
                // 显示具体事件
                if (result.is_happen && result.events.length > 0) {
                    result.events.forEach((event, index) => {
                        const confidencePercent = Math.round(event.confidence * 100);
                        let riskClass = '';
                        
                        if (event.event_level === '高风险') {
                            riskClass = 'high-risk';
                        } else if (event.event_level === '中风险') {
                            riskClass = 'medium-risk';
                        } else {
                            riskClass = 'low-risk';
                        }
                        
                        const eventCard = document.createElement('div');
                        eventCard.className = `event-card ${riskClass.replace('risk', '-risk')}`;
                        
                        eventCard.innerHTML = `
                            <div class="event-header">
                                <div class="event-type">${event.event_type}</div>
                                <div class="event-level ${riskClass}">${event.event_level}</div>
                            </div>
                            
                            <div class="event-details">
                                <div class="detail-item">
                                    <span class="detail-label">检测时间</span>
                                    <span class="detail-value">${event.event_time}</span>
                                </div>
                                <div class="detail-item">
                                    <span class="detail-label">位置描述</span>
                                    <span class="detail-value">${event.location_description}</span>
                                </div>
                                <div class="detail-item">
                                    <span class="detail-label">置信度</span>
                                    <div class="confidence-bar">
                                        <div class="confidence-fill" style="width: ${confidencePercent}%"></div>
                                        <div class="confidence-text">${confidencePercent}%</div>
                                    </div>
                                </div>
                                <div class="detail-item">
                                    <span class="detail-label">边界框中心</span>
                                    <span class="detail-value">(${event.normalized_box.x_center.toFixed(2)}, ${event.normalized_box.y_center.toFixed(2)})</span>
                                </div>
                            </div>
                            
                            <div class="event-description">
                                <strong>描述：</strong>${event.description}
                            </div>
                        `;
                        
                        eventsList.appendChild(eventCard);
                    });
                } else {
                    eventsList.innerHTML = `
                        <div class="no-events">
                            <p>✓ 图像中未检测到任何异常事件</p>
                        </div>
                    `;
                }
                
                resultsContainer.style.display = 'block';
                processBtn.disabled = false;
            }
        });
    </script>
</body>
</html>